// pages/oerder-detail/order-detail.js
const app=getApp();
var WxParse = require('../../wxParse/wxParse.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shopcar:[],
    numzong: 0,
    priceszong: 0,
    shop:{},
    shopspec: true,
    goodindex: 0,
    specac: [],
    specidac: [],
    foodid: '',//商品ID
  },
  /*单纯加入购物车*/
  shopcaradd: function (e) {
    var that = this;
    var shopcar = that.data.shopcar;
    // console.log(shopcar)
    var bol = true;
    var foodid = that.data.shop.category;
    var goodid = that.data.shop.id;
    if (shopcar.length == 0) {
      shopcar.push({
        foodid: foodid,
        goodid: goodid,
        price: that.data.shop.sale_price,
        type: 0,
        num: 0
      })
    }
    shopcar.forEach(function (item, index) {
      if (item.foodid == foodid && item.goodid == goodid) {
        item.num = ++item.num;
        bol = false;
      }
    })
    if (bol) {
      shopcar.push({
        foodid: foodid,
        goodid: goodid,
        price: that.data.shop.sale_price,
        type: 0,
        num: 1
      })
    }
    wx.showToast({
      title: '加入购物成功',
      icon: 'success'
    })
    that.setData({
      shopcar: shopcar
    })
    this.znumprice();
    wx.removeStorageSync('shopcar')
    wx.setStorageSync('shopcar', that.data.shopcar);
  },
  /*格式中的加入购物车 */
  shopcaradd2: function (e) {
    var that = this;
    var shopcar = that.data.shopcar;
    var bol = true;
    var foodid = that.data.shop.category;
    var goodid = that.data.shop.id;
    if (shopcar.length == 0) {
      shopcar.push({
        foodid: foodid,
        goodid: goodid,
        price: e.currentTarget.dataset.pri,
        type: 1,
        num: 0,
        specidac: that.data.specidac,
        specac: that.data.specac,
      })
    }
    shopcar.forEach(function (item, index) {
      if (item.foodid == foodid && item.goodid == goodid) {
        if (item.type == 1 && item.specidac.toString() == that.data.specidac.toString()) {
          item.num = ++item.num;
          bol = false;
        }
      }
    })
    if (bol) {
      shopcar.push({
        foodid: foodid,
        goodid: goodid,
        price: e.currentTarget.dataset.pri,
        type: 1,
        num: 1,
        specidac: that.data.specidac,
        specac: that.data.specac,
      })
    }
    that.setData({
      shopcar: shopcar,
      addnum: false
    })
    this.znumprice();
    wx.setStorageSync('shopcar', that.data.shopcar);
  },
  shopspechide: function (e) {
    this.setData({
      shopspec: true
    })
  },
  /*显示格式*/
  shopspecshow: function (e) {
    var specidac = [];
    var specac = [];
    var that = this;
    var bol = true;
    var foodid = that.data.shop.category;
    var goodid = that.data.shop.id;
    that.data.foodid = e.currentTarget.dataset.id,
      wx.request({
        url: app.globalData.directory + 'api/v2/getGoodsSpec/' + e.currentTarget.dataset.id,
        header: {
          'content-type': 'application/json',
          'token': wx.getStorageSync('token')
        },
        success: function (res) {
          if (res.data.status == 'ok') {
            var spec = res.data.data.model.spec;
            spec.forEach(function (items, index) {
              specac[index] = items.item[0].name,
                specidac[index] = items.item[0].id
            })
            spec.forEach(function (items, index) {
              specac[index] = items.item[0].name,
                specidac[index] = items.item[0].id
            })

            that.data.shopcar.forEach(function (item, index) {
              if (item.foodid == foodid && item.goodid == goodid) {
                if (item.type == 1 && item.specidac.toString() == specidac.toString()) {
                  bol = false;
                  specac = item.specac;
                  specidac = item.specidac;
                }
              }
            })
            if (!bol) {
              that.setData({
                addnum: false
              })
            } else {
              that.setData({
                addnum: true
              })
            }
            that.setData({
              shopspec: false,
              foodid: that.data.foodid,
              goodindex: e.currentTarget.id,
              specac: specac,
              specidac: specidac,
              foodspec: res.data.data,
              'foodspec.tit': e.currentTarget.dataset.tit
            })

          } else {
            wx.showToast({
              title: '请求失败,请稍候',
              icon: 'none',
              duration: 2000,
            })
          }
        }
      })
  },

  /*格式中价格的变化*/
  speccon: function (e) {
    var specidacstr = '';
    var that = this;
    var bol = true;
    var foodid = that.data.shop.category;
    var goodid = that.data.shop.id;
    this.data.specac[e.currentTarget.dataset.id] = e.currentTarget.dataset.text;
    this.data.specidac[e.currentTarget.dataset.id] = e.currentTarget.dataset.current;
    this.setData({
      specac: that.data.specac,
      specidac: that.data.specidac
    })
    that.data.shopcar.forEach(function (item, index) {
      if (item.foodid == foodid && item.goodid == goodid) {
        if (item.type == 1 && item.specidac.toString() == that.data.specidac.toString()) {
          bol = false;
        }
      }
    })
    if (!bol) {
      that.setData({
        addnum: false
      })
    } else {
      that.setData({
        addnum: true
      })
    }
    that.data.specidac.forEach(function (item, index) {
      specidacstr += '_' + item;
    })
    wx.request({
      url: app.globalData.directory + 'api/v2/getGoodsSpecPrice/',
      data: {
        'id': that.data.foodid,
        'spec_ids': specidacstr,
      },
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync('token')
      },
      success: function (res) {
        if (res.data.status == 'ok') {
          that.setData({
            'foodspec.price.price': res.data.data
          })

        } else {
          wx.showToast({
            title: '请求失败,请稍候',
            icon: 'none',
            duration: 2000,
          })
        }
      }
    })
  },
  shopspechidel: function (e) {
  },
  /*总的价格数量 */
  znumprice: function () {
    var znum = 0;
    var zprice = 0;
    if (!this.data.shopcar) return false;
    this.data.shopcar.forEach(function (item, index) {
      znum += item.num
      zprice += item.price * item.num
    })
    if (znum > 99) {
      znum = '99+'
    }
    zprice = parseInt(zprice * 100) / 100;
    this.setData({
      numzong: znum,
      priceszong: zprice,
    })
    wx.setStorageSync('priceszong', this.data.priceszong);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.request({
      url: app.globalData.directory + 'api/v2/getGoodsDetails/'+options.id,
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync('token')
      },
      success: function (res) {
        if (res.data.status == 'ok') {
          that.setData({
            shop: res.data.data
          })
          WxParse.wxParse('article_content', 'html', res.data.data.details, that, 5)
        } else {
          wx.showToast({
            title: '请求失败,请重试',
            icon: 'none',
            duration: 2000,
          })
        }
      }
    })
    if (typeof (wx.getStorageSync('shopcar')) !== 'string') {
      this.setData({
        shopcar: wx.getStorageSync('shopcar')
      })
    }
    this.znumprice();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (typeof (wx.getStorageSync('shopcar')) !== 'string') {
      this.setData({
        shopcar: wx.getStorageSync('shopcar')
      })
    }
    this.znumprice();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})